package com.woniu108.car.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.dto.CarSeriesYearDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.woniu108.car.model.CarSeriesYear;
import com.woniu108.car.param.InsertCarSeriesYearParam;
import com.woniu108.car.service.CarSeriesYearService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车系生产年份表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/carSeriesYear")
@Api(tags = "汽车年份管理")
public class CarSeriesYearController {
    @Resource
    private CarSeriesYearService carSeriesYearService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 查询所有年份表
     * @return 所有年份表
     */
    @GetMapping("/findAllCarSeriesYear.c")
    @ApiOperation("查询所有年份表")@AdminHistoryLog
    public Result<List<CarSeriesYearDto>> findAllCarSeriesYear() {
        List<CarSeriesYear> list = carSeriesYearService.list();
        List<CarSeriesYearDto> dtos = BeanUtil.copyToList(list, CarSeriesYearDto.class);
        return Result.newListInstance(CarSeriesYearDto.class).setData(dtos).setMsg("查询所有年份表成功");
    }
    /**
     * 新增
     * @param cSYear 年份
     * @return boolean值
     */
    @GetMapping("/insertCarSeriesYear.api")
    @ApiOperation("新增年份表")@AdminHistoryLog
    @ApiImplicitParam(name = "cSYear",value = "年份值")
    public Result<Boolean> insertCarSeriesYear(String cSYear, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        InsertCarSeriesYearParam param = new InsertCarSeriesYearParam(cSYear, adminId);
        Boolean bool = carSeriesYearService.insertCarSeriesYear(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增年份表成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增年份表失败").setData(false).setCode(500);

    }
}

